package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.jiayen.util.DisplayUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.util.L;

import org.xutils.common.util.DensityUtil;

/**
 * Created by jiayen on 16/10/12.
 * 删除原因dialog
 */

public class DelResonDialog extends Dialog implements View.OnClickListener {

    private DelDone delDone;
    private Context context;
    private Window dialogWindow;
    private WindowManager.LayoutParams lp;
    private int screenHgith;
    private int dialogHeigth;//对话框高度  这里默认设为120dp
    private int dialogWidth;//对话框宽度  这里默认设为180dp
    private int screenWidth;//屏幕宽带
    private View mView;

    private int bottomHeigth;//底部栏的高度  默认是50dp
    public DelResonDialog(Context context) {
        super(context);
        this.context = context;
        //获取屏幕的高度
        screenWidth=DensityUtil.getScreenWidth();
        screenHgith= DensityUtil.getScreenHeight();
        dialogHeigth= DisplayUtil.dip2px(context,160);
        dialogWidth= (int) (screenWidth*0.92);//DisplayUtil.dip2px(context,220);
        bottomHeigth=DisplayUtil.dip2px(context,60);

//        mView = LayoutInflater.from(context).inflate(R.layout.toutiao_select_dialog,null);
    }

    public void setDelDone(DelDone delDone) {
        this.delDone = delDone;
    }

    public DelResonDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    protected DelResonDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        setContentView(mView);
////            dialogWindow = getWindow();
////            lp = dialogWindow.getAttributes();
////            dialogWindow.setBackgroundDrawableResource(R.color.translate); //设置对话框背景为透明
////            DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
////        lp.width = dialogWidth; //
////            lp.width= dialogWidth;
////            lp.height=dialogHeigth;
////            dialogWindow.setAttributes(lp);
//        init();
    }

    private void init() {

        findViewById(R.id.old_btn).setOnClickListener(this);
        findViewById(R.id.bad_btn).setOnClickListener(this);
        findViewById(R.id.looked_btn).setOnClickListener(this);
        findViewById(R.id.not_like_btn).setOnClickListener(this);
        findViewById(R.id.content_not_accurate_btn).setOnClickListener(this);
        findViewById(R.id.content_error_btn).setOnClickListener(this);
    }

    /**
     * closeType	0	不感兴趣
     * closeType	1	重复旧闻
     * closeType	2	内容质量差
     * closeType	3	来源
     * closeType	4	标签
     * closeType	5	已看过
     *
     * @param view
     */

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.not_like_btn:
                handReson(0);
                break;
            case R.id.old_btn:
                handReson(1);
                break;
            case R.id.bad_btn:
                handReson(2);
                break;
            case R.id.looked_btn:
                handReson(5);
                break;
            case R.id.content_not_accurate_btn:
                handReson(6);
                break;
            case R.id.content_error_btn:
                handReson(7);
                break;
            default:
                break;
        }
    }

    private void handReson(int type) {
        if (delDone != null) delDone.done(type);
    }

    public interface DelDone {
        void done(int type);
    }

    public void show(int x, int y) {
        L.e("show","======x="+x+"=======y="+y);
        windowDeploy(x, y);
        setCanceledOnTouchOutside(true);
        show();
    }

    //设置窗口显示
    private void windowDeploy(int x, int y) {
        if(mView==null) {
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            mView = LayoutInflater.from(context).inflate(R.layout.toutiao_select_dialog,null);//第一次先初始化
            setContentView(mView);
            init();
        }
        dialogWindow = getWindow(); //得到对话框
        dialogWindow.setGravity(Gravity.LEFT|Gravity.TOP);

        dialogWindow.setBackgroundDrawableResource(R.color.translate); //设置对话框背景为透明
        WindowManager.LayoutParams wl = dialogWindow.getAttributes();
//        int width= wl.width;
        int heigth =dialogHeigth;//对话框的高度
        //根据x，y坐标设置窗口需要显示的位置
        wl.x = x-dialogWidth-20; //x小于0左移，大于0右移
        if(screenHgith-y>heigth+bottomHeigth){//说明底部有空间显示dialog
            if(wl.x > 0) {
                mView.setBackgroundResource(R.mipmap.toutiaoyuanjiaojuxing);
                dialogWindow.setWindowAnimations(R.style.dialog_anim); //设置窗口弹出动画
            } else{
                mView.setBackgroundResource(R.drawable.headline_list_three);
                dialogWindow.setWindowAnimations(R.style.dialog_anim_1); //设置窗口弹出动画
            }
            wl.y = y; //y小于0上移，大于0下移
        }else{//说明空间不足则把dialog显示在上方
            if(wl.x > 0) {
                mView.setBackgroundResource(R.drawable.top_dialog);
                dialogWindow.setWindowAnimations(R.style.dialog_anim_top); //设置窗口弹出动画
            } else{
                mView.setBackgroundResource(R.drawable.headline_list_three_fan);
                dialogWindow.setWindowAnimations(R.style.dialog_anim_top_1); //设置窗口弹出动画
            }
            wl.y = y-heigth-35; //y小于0上移，大于0下移
        }

//        if(wl.x>0){
//            wl.width=dialogWidth;
//        }else{
//            wl.x=20;
//            wl.width=x-22;
//        }
        wl.x = (screenWidth - dialogWidth)/2;
        wl.width = dialogWidth;

        wl.height=dialogHeigth;
        //            wl.alpha = 0.6f; //设置透明度
        //            wl.gravity = Gravity.BOTTOM; //设置重力
        dialogWindow.setAttributes(wl);
    }
}
